package com.shanghaiyimu.yimiyangche.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.BiaoZhunListViewAdapter;
import com.shanghaiyimu.yimiyangche.adapter.BiaoZhunPopAdapter;
import com.shanghaiyimu.yimiyangche.bean.ShopBean;

import java.util.ArrayList;
import java.util.List;

public class BiaoZhunWashCarActivity extends AppCompatActivity  implements View.OnClickListener{
    private TextView textView1,textView2,textView3,txt1,txt2,txt3,txt4;
    private ListView mListView,popListView;
    private PopupWindow popupWindow = null;
    private PopupWindow popupWindow1 = null;
    private PopupWindow popupWindow2 = null;
    private List<String> data,data1;
    private List<ShopBean> list;
    private BiaoZhunListViewAdapter mAdapter;
    private ImageView mImageView;
    private View view;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_biao_zhun_wash_car);

        initView();
        initData();
    }

    private void initData() {

        for (int i = 0; i < 10; i++) {
            ShopBean shopBean = new ShopBean();
            shopBean.setName(i+"号商铺");
            shopBean.setPrice(i+"10");
            list.add(shopBean);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void initView() {
        view = LayoutInflater.from(this).inflate(R.layout.biaozhun_pop_item,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow2.dismiss();
            }
        });
        txt1 = (TextView) view.findViewById(R.id.text);
        txt2 = (TextView) view.findViewById(R.id.text1);
        txt3 = (TextView) view.findViewById(R.id.text2);
        txt4 = (TextView) view.findViewById(R.id.text3);
        txt1.setOnClickListener(this);
        txt2.setOnClickListener(this);
        txt3.setOnClickListener(this);
        txt4.setOnClickListener(this);

        popupWindow2 = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        // 使其聚集,防止无法关闭popupWindow
        popupWindow2.setFocusable(true);
        popupWindow2.setTouchable(true);
        // 设置允许在外点击消失
        popupWindow2.setOutsideTouchable(true);
        ColorDrawable colorDrawable = new ColorDrawable(0x99000000);
        popupWindow2.setBackgroundDrawable(colorDrawable);


        mImageView = (ImageView) findViewById(R.id.biaozhun_bar_img_back);
        mImageView.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.biaozhun_washcar_lv);
        list = new ArrayList<>();
        mAdapter = new BiaoZhunListViewAdapter(this,list);
        mListView.setAdapter(mAdapter);

        textView3 = (TextView) findViewById(R.id.paixuxuanze_biaozhun);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow2.showAsDropDown(textView3);
            }
        });


        textView2 = (TextView) findViewById(R.id.biaozhun_tv_xiala);
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (popupWindow1!=null&&popupWindow1.isShowing()){
                    popupWindow1.dismiss();
                }
                else {
                    showWindow2(textView2);
                }
            }
        });


        textView1 = (TextView) findViewById(R.id.biaozhun_washcar_txt1);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              if (popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                else {
                    showWindow(textView1);
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BiaoZhunWashCarActivity.this,ShopContentActivity.class);
                startActivity(intent);
            }
        });

    }

    private void showWindow(View parent) {
        if (popupWindow == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
            popListView = (ListView) view.findViewById(R.id.pop_view_lv);
            data = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                data.add("西安市" + i);
            }
            BiaoZhunPopAdapter adapter = new BiaoZhunPopAdapter(data, this);
            popListView.setAdapter(adapter);
            popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
        }
        // 使其聚集,防止无法关闭popupWindow
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        ColorDrawable colorDrawable = new ColorDrawable(0x99000000);
        popupWindow.setBackgroundDrawable(colorDrawable); //设置背景显示图片

        /**
         * 在某个控件的下方展示
         * anchor： 	指定控件
         * x,y: 		偏移量
         */
        popupWindow.showAsDropDown(parent,0,0);
        popListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                textView1.setText(data.get(i));
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.biaozhun_bar_img_back:
                finish();
                break;
            case R.id.text:
                textView3.setText(txt1.getText());
                popupWindow2.dismiss();
                break;
            case R.id.text1:
                textView3.setText(txt2.getText());
                popupWindow2.dismiss();
                break;
            case R.id.text2:
                textView3.setText(txt3.getText());
                popupWindow2.dismiss();
                break;
            case R.id.text3:
                textView3.setText(txt4.getText());
                popupWindow2.dismiss();
                break;

        }
    }
    private void showWindow2(View parent) {
        if (popupWindow1 == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
            popListView = (ListView) view.findViewById(R.id.pop_view_lv);
            data1 = new ArrayList<>();
                data1.add("标准洗车");
                data1.add("全车清洗");
                data1.add("内饰清洗");
                data1.add("全车打蜡");
                data1.add("全车镀晶");
                data1.add("全车贴膜");
                data1.add("四轮定位");
                data1.add("底盘装甲");
                data1.add("玻璃镀膜");
                data1.add("空气净化");
            BiaoZhunPopAdapter adapter = new BiaoZhunPopAdapter(data1, this);
            popListView.setAdapter(adapter);
            popupWindow1 = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        }
        // 使其聚集,防止无法关闭popupWindow
        popupWindow1.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow1.setOutsideTouchable(true);
        ColorDrawable colorDrawable = new ColorDrawable(0x99000000);
        popupWindow1.setBackgroundDrawable(colorDrawable); //设置背景显示图片

        /**
         * 在某个控件的下方展示
         * anchor： 	指定控件
         * x,y: 		偏移量
         */
        popupWindow1.showAsDropDown(parent,0,0);
        popListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                textView2.setText(data1.get(i));
                if (popupWindow1 != null) {
                    popupWindow1.dismiss();
                }
            }
        });
    }

}
